package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.RoleResourcesService;
import com.yixun.qfbao.apidto.GetAgentListResDto;
import com.yixun.qfbao.apidto.GetAgentListReqDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.*;
import com.yixun.qfbao.model.dto.CompanyDto;
import com.yixun.qfbao.model.dto.CompanyRelationDto;
import com.yixun.qfbao.model.enums.MechanismAccountTypeEnum;
import com.yixun.qfbao.service.CompanyRelationService;
import com.yixun.qfbao.service.CompanyService;
import com.yixun.qfbao.service.PartnerInfoService;
import com.yixun.qfbao.vo.GetAgentListReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @classname: GetAgentListHander.java
 * @author: Beau
 * @create: 2019/12/20 11:15
 * @version: 1.0.0
 **/
@Service("getAgentListHander")
public class GetAgentListHander extends CommonAbstractHander {
    @Autowired
    private PartnerInfoService partnerInfoService;

    @Autowired
    private RoleResourcesService roleResourcesService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private CompanyRelationService companyRelationService;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        GetAgentListReqVo reqVo = (GetAgentListReqVo) reqDto;
        GetAgentListReqDto dto = reqVo.getParams();
        BaseResponseDTO<GetAgentListResDto> res = new BaseResponseDTO<>();
        GetAgentListResDto resDto = new GetAgentListResDto();
        CompanyRelationDto relationDto = new CompanyRelationDto();
        List<String> resourcesCodes = new ArrayList<>();
        String userId = dto.getUserId();
        PartnerInfo partnerInfo = partnerInfoService.selectById(Long.valueOf(userId));
        if (partnerInfo == null) {
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("未找到用户信息");
            return res;
        }
        resourcesCodes.add("menu_0003_0003_0010");
        List<RoleResources> roleResources = roleResourcesService.listRoleResources(partnerInfo.getUserRoleId(), resourcesCodes);
        if (roleResources.size() > 0 || (MechanismAccountTypeEnum.mechanism.getNo()).equals(partnerInfo.getAccountType())) {
            CompanyDto companyDto = new CompanyDto();
            relationDto.setDevCompany(partnerInfo.getHigherid());
            List<CompanyRelation> companyRelations = companyRelationService.selectListByDto(relationDto);
            List<Long> agentIds = companyRelations.stream().map(CompanyRelation::getAgentCompany).collect(Collectors.toList());
            companyDto.setIds(agentIds);
            List<CompanyDto> agentList = companyService.selectListByIds(companyDto);
            resDto.setAgentCompanyList(agentList);
            res.setResData(resDto);
        } else {
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("用户无查看分销机构权限");
        }
        return res;
    }
}
